Thread: [beginner] creating functions in a program

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    21

    [beginner] creating functions in a program

    I've just been having a little mess about and thought that I'd try and make a little program that takes input from the user and puts it into an array. This lead me to this information about fgets() and this post that has some code example on it.

    But I used this to make a little program that works, and I was just wondering about how I could refine it and make the 'design' aspect of it better, I haven't made any functions yet.

    here's the code though :

    heres a link if you'd rather look at it on pastebin with syntax highlighting!


    Code:
    /*
    
    
    Name & Phone number program 
    
    
    
    
    Needs a total of 2 arrays, one for the name and one for the phone number.  
    
    
    
    
    */
    
    
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
        int i;
        char names[5][20];
        char phoneNumber[5][11];
        int select; 
        
        printf("Please enter names : \n\n");
        
        
        for ( i = 0 ; i < 5 ; i++ ) 
        {
            fgets( names[i], sizeof names[i], stdin );
            names[i][strlen(names[i]) - 1] = '\0'; //just subtract 1 to overwrite the '\n' with null terminator
        }
        
        puts("Please enter phone numbers : ");
        
        for ( i = 0 ; i < 5 ; i++ ) 
        {
            fgets( phoneNumber[i], sizeof phoneNumber[i], stdin );
            phoneNumber[i][strlen(phoneNumber[i]) - 1] = '\0'; //just subtract 1 to overwrite the '\n' with null terminator
        }
        
        puts("Which array would you like? ");
        
        scanf("%i", &select);
        
        printf("name %i is   : %s\n", select, names[select]);
        printf("number %i is : %s\n", select, phoneNumber[select]);
        
        printf("\n");
        
        return 0;
    
    
    }
    So I'm thinking about using functions in the program, but I'm not too sure where I'd do that in this example (maybe it's too small to do practically?)

    The two for loops are fairly dependant on the actual code around them, and I was thinking that perhaps the functions should be free? I'm not sure why I think that.

    So it's the design element that I'm trying to focus on with it at the moment, although any other advice would be much appreciated.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Well the first 2 "for" loops are just about identical, you I suppose that you can make a the contents of them a function (note that you can not use sizeof in another function, so you will have to pass that as a parameter). To put some strength in your code, you could check to see if fgets succeeded - And you can check to see if the input was too long by checking to see if '\n' is in the array. If this fails, you could return a -1 from a function and let your main try again.

    You could move the last 3 printf's into 1.

    You could check to see if the array select at the bottom is in the correct range before using it (i.e. what if I put in 42 by accident?)

    Basically, I would work on making your code stronger.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Some rules of thumb I have:

    1. Anything I'm going to do more than once should be its own function.

    2. One function does one thing. If that "thing" can be logically broken down into smaller pieces, each piece should be a function, then the original function just calls them. For example, maybe I have an application that selects a collection of data, filters it, and then formats the filtered results for display. I would make each of these a function, even if I am not yet going to reuse any of them, because later when I decide I want a different option for a display - or a different collection of data to start from - I can leverage much of what I already have.

    3. If a function is more than about 15-20 lines, it's probably something I should be decomposing into smaller pieces.

    Some of this depends on context. On certain systems where performance is low and the overhead of a function call is high (e.g. some embedded systems), or where there were restrictions (e.g. low maximum number of stack levels) I would code differently.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Spray View Post
    So I'm thinking about using functions in the program, but I'm not too sure where I'd do that in this example (maybe it's too small to do practically?)
    Rewrite the part you want as if you had some functions already written. Next you can supply definitions for those functions and make sure the behaviour is the same. Here's one possibility

    Code:
    int main(void)
    {
        char names[5][20];
        char phoneNumber[5][11];
        int select; 
         
        ask_data(names, 5, sizeof names[0], "Please enter names : \n\n");
        ask_data(phoneNumber, 5, sizeof phoneNumber[0], "Please enter phone numbers : ");
        ask_array(&select, 1, 9999, "Which array would you like? ");
         
        printf("name %i is   : %s\n", select, names[select]);
        printf("number %i is : %s\n", select, phoneNumber[select]);     
        printf("\n");
         
        return 0; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-16-2014, 11:22 AM
  2. Beginner with Problems creating lists
    By mactoni in forum C++ Programming
    Replies: 1
    Last Post: 10-16-2013, 11:29 PM
  3. Creating Functions For My Program
    By nelly26 in forum C Programming
    Replies: 3
    Last Post: 04-03-2012, 02:24 AM
  4. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  5. Need some help with a beginner functions program!
    By nelledawg in forum C Programming
    Replies: 5
    Last Post: 03-03-2008, 07:05 AM